//
//  UIView+JTExtension.m
//  百思不得姐
//
//  Created by sun lei on 16/11/15.
//  Copyright © 2016年 BeiJingWangDianJieTong. All rights reserved.
//

#import "UIView+JTExtension.h"

@implementation UIView (JTExtension)

/******************* 一.扩展属性 - 方便开发 *******************/
/** 大小 **/
- (CGSize)jt_size {
    
    return self.frame.size;
}

- (void)setJt_size:(CGSize)jt_size {
    
    CGRect frame = self.frame;
    frame.size = jt_size;
    [self setFrame:frame];
}

/** 宽 **/
- (CGFloat)jt_width {
    
    return self.frame.size.width;
}

- (void)setJt_width:(CGFloat)jt_width {
    
    CGRect frame = self.frame;
    frame.size.width = jt_width;
    [self setFrame:frame];
}

/** 高 **/
- (CGFloat)jt_height {
    
    return self.frame.size.height;
}

- (void)setJt_height:(CGFloat)jt_height {
    
    CGRect frame = self.frame;
    frame.size.height = jt_height;
    [self setFrame:frame];
}

/** x **/
- (CGFloat)jt_x {
    
    return self.frame.origin.x;
}

- (void)setJt_x:(CGFloat)jt_x {
    
    CGRect frame = self.frame;
    frame.origin.x = jt_x;
    [self setFrame:frame];
}

/** y **/
- (CGFloat)jt_y {
    
    return self.frame.origin.y;
}

- (void)setJt_y:(CGFloat)jt_y {
    
    CGRect frame = self.frame;
    frame.origin.y = jt_y;
    [self setFrame:frame];
}

/** centerX **/
- (CGFloat)jt_centerX {
    
    return self.center.x;
}

- (void)setJt_centerX:(CGFloat)jt_centerX {
    
    CGPoint center = self.center;
    center.x = jt_centerX;
    [self setCenter:center];
}

/** centerY **/
- (CGFloat)jt_centerY {
    
    return self.center.y;
}

- (void)setJt_centerY:(CGFloat)jt_centerY {
    
    CGPoint center = self.center;
    center.y = jt_centerY;
    [self setCenter:center];
}

/** right **/
- (CGFloat)jt_right {
    
    return CGRectGetMaxX(self.frame);
}

- (void)setJt_right:(CGFloat)jt_right {
    
    [self setJt_x:jt_right - self.jt_width];
}

/** bottom **/
- (CGFloat)jt_bottom {
    
    return CGRectGetMaxY(self.frame);
}

- (void)setJt_bottom:(CGFloat)jt_bottom {
    
    [self setJt_y:jt_bottom - self.jt_height];
}

/************** 二.在xib中加载跟类名一模一样的控件 **************/
+ (nullable instancetype)jt_viewWithXib {
    
    return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil].firstObject;
}

/************** 三.查找view中某一类型的所有子控件 **************/
/**
 查找 view 中所有的 Class 类型的子控件 view

 @param class 想获得的控件类型(若为nil则返回UIView类型的控件)
 @param view 根控件
 @param mutableArray 会为该array添加所有符合类型的view(若为空则什么都不做)
 */
+ (void)jt_findViewWithClass:(Class _Nullable)aClass inView:(UIView * _Nullable)view mutableArray:(NSMutableArray * _Nullable)mutableArray {
    
    if (!mutableArray) return;
    if (!aClass) aClass = [UIView class];
    
    for (UIView *subView in view.subviews) {
        
        [self jt_findViewWithClass:aClass inView:subView mutableArray:mutableArray];
    }
    
    // 判断view是否为 aClass 类型
    if ([view isKindOfClass:aClass]) [mutableArray addObject:view];
}

/************** 四.判断两个控件是否重叠 **************/
- (BOOL)jt_intersectWithOtherView:(UIView * _Nullable)otherView {
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    // 判断 自身view 是否与 otherView 有重叠
    CGRect selfRect = [self convertRect:self.bounds toView:window];
    CGRect otherViewRect = [otherView convertRect:otherView.bounds toView:window];
    return CGRectIntersectsRect(selfRect, otherViewRect);
}

@end
